package cn.ainit.yz.sprk;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.common.ReturnObject;
import cn.ainit.sys.ds.store.StoreService;
import cn.ainit.sys.query.datastore.DataStore;
import cn.ainit.sys.utils.UUID32;
import cn.ainit.yz.sprk.entity.Sprk;
import cn.ainit.yz.sprk.entity.SprkItem;
import cn.ainit.yz.sprk.entity.SprkObj;

@Controller
@RequestMapping("/sprk")
public class SprkControllor {
	SprkService sprkService;
	
	@Resource
	public void setSprkService(SprkService sprkService) {
		this.sprkService = sprkService;
	}
	private StoreService storeService;

	@Resource
	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}
	
	@RequestMapping("/saveSprk")
	@ResponseBody
	public ReturnObject saveSprk(@RequestBody SprkObj sprkObj, HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		Sprk sprk = sprkObj.getSprk();
		List<SprkItem> sprkItems = sprkObj.getSprkItems();

		try {
			System.out.println("sprkId:"+sprk.getSpId());
			if (sprk.getSpId() != null && !sprk.getSpId().equals("")) {
				//删除旧的不存在的记录    条件  sp_id 和 sp_item_id
				if(sprkService.updateSprk(sprk,sprkItems)){
					returnObject.setMsg("upsuccess");
				}else{
					returnObject.setMsg("upfail");
				}
				
			}else{
				String sprkId = UUID32.newGuid();
				sprk.setSpId(sprkId);
				//保存操作
				if(sprkService.saveSprk(sprk,sprkItems)){
					returnObject.setMsg("success");
					returnObject.setInfo(sprkId);
				}else{
					returnObject.setMsg("fail");
				}
			}
		} catch (Exception e) {
			returnObject.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return returnObject;
	}
	/**
	 * 印厂入库单生成到货单
	 * @param exId
	 * @param oId
	 * @param dsId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 */
	@RequestMapping("/sprkToDaohuo") 
	@ResponseBody
	public ReturnObject newDaohuo(String sprkId,String oId,String dsId,HttpServletRequest request) throws Exception{
		ReturnObject returnObject = new ReturnObject();
		try {
			//执行存储过程
			String jhId = this.sprkService.sprkToDaohuo(sprkId,oId);
			returnObject.setInfo("JH_ID-"+jhId);
			String[] dsIds = dsId.split(";"); 
			DataStore dataStore = storeService.findStore(dsIds[0]);
			returnObject.setDynObj(dataStore);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnObject ;
	}
	/**
	 * 根据选题Id得到商品Id
	 * @param xtId
	 * @param request
	 * @return
	 * @throws Exception
	 * @author wyang
	 */
	@RequestMapping("/getHIdByXtId")
	@ResponseBody
	public String getHIdByXtId(String xtId,HttpServletRequest request) throws Exception{
		String hId = "";
		try {
			hId = this.sprkService.getHId(xtId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hId;
	}
}
